<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.gxgyl.com All rights reserved.

// +----------------------------------------------------------------------
// | Author: Liuyu
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\controller\shiyao;

use app\api\controller\Controller;
use app\api\model\shiyao\Crc as CenterCrcModel;
use app\api\model\shiyao\Pi as CenterPiModel;
use app\api\model\shiyao\Project as ProjectModel;
use app\api\model\shiyao\ProjectPay as ProjectPayModel;
use app\api\service\User as UserService;
use cores\exception\BaseException;
use think\facade\Db;
use think\response\Json;

/**
 * 管理
 * Class
 * @package app\api\controller
 */
class Project extends Controller {

	public function listAll(): Json {
		// 获取列表
		$model = new ProjectModel;
		$list = $model->getListAll($this->request->param());
		return $this->renderSuccess(compact('list'));
	}

	public function listPm($projectId): Json {
		$list = Db::name('shiyao_project_pm')->alias('pm')->join('user', 'user.user_id=pm.pm_id')
		->where('project_id',$projectId)
			->field('user.nick_name as label,pm.pm_id as value')
			->select()->toArray();
		return $this->renderSuccess(compact('list'));
	}

	/**
	 * PI中心列表
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function listPi(): Json {
		// 获取列表
		$model = new CenterPiModel;
		$list = $model->getList($this->request->param());
		return $this->renderSuccess(compact('list'));
	}

	/**
	 * 列表
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function list(): Json {

		$user = UserService::getCurrentLoginUser(true);

		$role = $user['grade'] ? $user['grade']['grade_id'] : 0;

		if ($role == 2) {
			$model = new CenterPiModel;
			$list = $model->getList($this->request->param());
		} elseif ($role == 1) {
			$model = new CenterCrcModel;
			$list = $model->getList($this->request->param());
		} elseif ($role == 0) {

			$list = [];
		} else {
			//列表
			$model = new ProjectModel;
			$list = $model->getList($this->request->param());
		}

		return $this->renderSuccess(compact('list', 'role'));
	}

	public function detailPm(int $projectId): Json {
		$detail = ProjectModel::detail($projectId);
		return $this->renderSuccess(compact('detail'));
	}

	/**
	 * 详情
	 * @param int $projectId 地址ID
	 * @return Json
	 * @throws BaseException
	 */
	public function detail(int $projectId): Json {
		$detail = ProjectModel::detail($projectId);
		return $this->renderSuccess(compact('detail'));
	}

	/*
		 *提交支付申请
	*/

	public function payApply(): Json {
		$model = new ProjectPayModel;
		if ($model->payApply($this->postForm())) {
			return $this->renderSuccess([], '提交成功');
		}
		return $this->renderError($model->getError() ?: '添加失败');
	}

	/**
	 * 添加
	 * @return Json
	 * @throws BaseException
	 */
	public function add(): Json {
		$model = new ProjectModel;
		if ($model->add($this->postForm())) {
			return $this->renderSuccess([], '添加成功');
		}
		return $this->renderError($model->getError() ?: '添加失败');
	}

	/**
	 * 编辑收货地址
	 * @param int $projectId 地址ID
	 * @return Json
	 * @throws BaseException
	 */
	public function edit(int $projectId): Json {
		$model = ProjectModel::detail($projectId);
		$pay = Db::name('shiyao_project_pay_log')->where('project_id', $projectId)->find();
		if ($pay) {
			return $this->renderError('有补助申请，不能编辑');
		}

		if ($model->edit($this->postForm())) {
			return $this->renderSuccess([], '更新成功');
		}
		return $this->renderError($model->getError() ?: '更新失败');
	}

	/**
	 * 设为默认地址
	 * @param int $projectId 地址ID
	 * @return Json
	 * @throws BaseException
	 */
	public function setDefault(int $projectId): Json {
		$model = ProjectModel::detail($projectId);
		if ($model->setDefault((int) $model['address_id'])) {
			return $this->renderSuccess([], '设置成功');
		}
		return $this->renderError($model->getError() ?: '设置失败');
	}

	/**
	 * 删除收货地址
	 * @param int $projectId 地址ID
	 * @return Json
	 * @throws BaseException
	 */
	public function remove(int $projectId): Json {
		$model = ProjectModel::detail($projectId);
		if ($model->remove()) {
			return $this->renderSuccess([], '删除成功');
		}
		return $this->renderError($model->getError() ?: '删除失败');
	}
}
